package com.example.user.controller;

import com.example.user.bo.Person01BO;
import com.example.user.service.UserService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;


@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/getAll1",method = RequestMethod.GET)
    public List<Person01BO> getAllPerson01BO1(){
        List<Person01BO> allPerson01BO = userService.getList1();
        return allPerson01BO;
    }
    @ResponseBody
    @RequestMapping(value = "/getAll2",method = RequestMethod.GET)
    public List<Person01BO> getAllPerson01BO2(){
        List<Person01BO> allPerson01BO = userService.getList2();
        return allPerson01BO;
    }
    @ResponseBody
    @RequestMapping(value = "/insert/{id}",method = RequestMethod.GET)
    public void insert(@PathVariable("id")String id){
        Person01BO person01BO = new Person01BO();
        person01BO.setId(id);
        int i = new Random().nextInt(10);
        person01BO.setSapName("张"+id);
        userService.insert(person01BO);
    }

    @ResponseBody
    @RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
    public List<Person01BO> select(HttpServletRequest request, @PathVariable("id")String id){
        AbstractApplicationContext ctx = (AbstractApplicationContext)WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
        System.out.println("get ctx.userServiceBean by request:" + ctx.getBean("userService"));
        List<Person01BO> allPerson01BO = userService.select(id);
        return allPerson01BO;
    }

    @ResponseBody
    @RequestMapping(value = "/check",method = RequestMethod.GET)
    public List<Person01BO> select(String sapId){
        return userService.check(sapId);
    }

    @ResponseBody
    @RequestMapping(value = "/check1",method = RequestMethod.GET)

    public Person01BO select1(String sapId){
        return userService.check1(sapId);
    }
}

